package com.lk.model;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author : lk
 * @date 2021/11/20 17:15
 **/
@EqualsAndHashCode(callSuper = true)
@Data
public class LoginUser extends User implements UserDetails {

    private Set<String> menuList;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        for (String permission : menuList) {
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permission);
            grantedAuthorities.add(authority);
        }
        return grantedAuthorities;
    }


    @Override
    public boolean isAccountNonExpired() {
        return this.getAccountNonExpired() == 0;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.getAccountNonExpired() == 0;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.getCredentialsNonExpired() == 0;
    }

    @Override
    public boolean isEnabled() {
        return this.getEnabled() == 0;
    }
}
